package com.tiger.lbs;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class DrivingRoute {
	private int distance;	// 米
	private int duration;	// 秒
	private List<DrivingStep> steps;
	
	public int getDistance() {
		return distance;
	}
	public void setDistance(int distance) {
		this.distance = distance;
	}
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
	public List<DrivingStep> getSteps() {
		return steps;
	}
	public void setSteps(List<DrivingStep> steps) {
		this.steps = steps;
	}
	
	public int getClosestStepIndex(Coordinate coord, int radius) {
		double minDistance = Double.MAX_VALUE;
		int closestIndex = -1;
		for(int i = 0; i < steps.size(); i++) {
			DrivingStep step = steps.get(i);
			double d = coord.distance(step.getStepOriginLocation(), step.getStepDestinationLocation());
			if(d <= radius && d < minDistance) {
				minDistance = d;
				closestIndex = i;
			}
		}
		return closestIndex;
	}
	public DrivingStep getClosestStep(Coordinate coord, int radius) {
		double minDistance = Double.MAX_VALUE;
		DrivingStep closestStep = null;
		for(DrivingStep step : steps) {
			double d = coord.distance(step.getStepOriginLocation(), step.getStepDestinationLocation());
			if(d <= radius && d < minDistance) {
				minDistance = d;
				closestStep = step;
			}
		}
		return closestStep;
	}
	
	public List<Coordinate> getPath() {
		List<Coordinate> path = new ArrayList<>();
		for(DrivingStep step : steps) {
//			if(step.getType() == 3) {
//				continue;
//			}
			if(path.isEmpty()) {
				path.add(step.getStepOriginLocation());
			}
			String p = step.getPath();
			if(!StringUtils.isEmpty(p)) {
				String[] ps = p.split(";|,");
				for(int i = 1; i < ps.length; i+=2) {
					Coordinate c = new Coordinate(new BigDecimal(ps[i - 1]).doubleValue(), new BigDecimal(ps[i]).doubleValue());
					if(path.get(path.size() - 1).isInRangeOf(c, 100)) {
						continue;
					}
					path.add(c);
				}
			}
			path.add(step.getStepDestinationLocation());
		}
		return path;
	}
}
